package cn.edu.neusoft.wuguangsheng.flowerimpression.fragment;


import android.app.Dialog;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.DialogThridUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.FKnowListActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.WeiboDialogUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.FKnowAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.FKnowBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.FKnowListModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShouYFragment extends BaseFragment implements View.OnClickListener {

    LinearLayout fknow_yhzs,fknow_drzw,fknow_zwkp,fknow_lssh,fknow_bhfz,fknow_hmpj,fknow_hdhy,fknow_zwys;
    RecyclerView shouye_rv;
    private boolean up = false;
    private List<FKnowBean> items;
    private Dialog mDialog;

    @Override
    void initView() {
        fknow_yhzs = (LinearLayout)view.findViewById(R.id.fknow_yhzs);
        fknow_drzw = (LinearLayout)view.findViewById(R.id.fknow_drzw);
        fknow_zwkp = (LinearLayout)view.findViewById(R.id.fknow_zwkp);
        fknow_lssh = (LinearLayout)view.findViewById(R.id.fknow_lssh);
        fknow_bhfz = (LinearLayout)view.findViewById(R.id.fknow_bhfz);
        fknow_hmpj = (LinearLayout)view.findViewById(R.id.fknow_hmpj);
        fknow_hdhy = (LinearLayout)view.findViewById(R.id.fknow_hdhy);
        fknow_zwys = (LinearLayout)view.findViewById(R.id.fknow_zwys);
        shouye_rv = (RecyclerView)view.findViewById(R.id.shouye_rv);
        mDialog = WeiboDialogUtils.createLoadingDialog(getContext(), "获取中...");
        FKnowListModel fKnowListModel = new FKnowListModel();
        fKnowListModel.getAllFknowList(new ListListener<FKnowBean>() {
            @Override
            public void onResponse(List<FKnowBean> list) {
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                shouye_rv.setLayoutManager(linearLayoutManager);
                items=list;
                FKnowAdapter fKnowAdapter=new FKnowAdapter(getContext(),items,R.layout.modeuleitem);
                shouye_rv.setAdapter(fKnowAdapter);
                shouye_rv.setItemAnimator(new DefaultItemAnimator());
                DialogThridUtils.closeDialog(mDialog);
            }

            @Override
            public void onFail(String msg) {
                DialogThridUtils.closeDialog(mDialog);

            }
        });

    }

    @Override
    void initEvent() {
        fknow_yhzs.setOnClickListener(this);
        fknow_drzw.setOnClickListener(this);
        fknow_zwkp.setOnClickListener(this);
        fknow_lssh.setOnClickListener(this);
        fknow_bhfz.setOnClickListener(this);
        fknow_hmpj.setOnClickListener(this);
        fknow_hdhy.setOnClickListener(this);
        fknow_zwys.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fknow_yhzs:
                Intent intent = new Intent(getActivity(), FKnowListActivity.class);
                intent.putExtra("title","养花知识");
                getContext().startActivity(intent);
                break;
            case R.id.fknow_drzw:
                Intent intent1 = new Intent(getActivity(), FKnowListActivity.class);
                intent1.putExtra("title","多肉植物");
                getContext().startActivity(intent1);
                break;
            case R.id.fknow_zwkp:
                Intent intent2 = new Intent(getActivity(), FKnowListActivity.class);
                intent2.putExtra("title","植物科普");
                getContext().startActivity(intent2);
                break;
            case R.id.fknow_lssh:
                Intent intent3 = new Intent(getActivity(), FKnowListActivity.class);
                intent3.putExtra("title","绿色生活");
                getContext().startActivity(intent3);
                break;
            case R.id.fknow_bhfz:
                Intent intent4 = new Intent(getActivity(), FKnowListActivity.class);
                intent4.putExtra("title","病害防治");
                getContext().startActivity(intent4);
                break;
            case R.id.fknow_hmpj:
                Intent intent5 = new Intent(getActivity(), FKnowListActivity.class);
                intent5.putExtra("title","花木盆景");
                getContext().startActivity(intent5);
                break;
            case R.id.fknow_hdhy:
                Intent intent6 = new Intent(getActivity(), FKnowListActivity.class);
                intent6.putExtra("title","花店花艺");
                getContext().startActivity(intent6);
                break;
            case R.id.fknow_zwys:
                Intent intent7 = new Intent(getActivity(), FKnowListActivity.class);
                intent7.putExtra("title","植物养生");
                getContext().startActivity(intent7);
                break;
        }
    }

    public ShouYFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_shou_y);
        return super.onCreateView(inflater,container,savedInstanceState);
    }
    @Override
    public void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }

}
